
# -*- coding: utf-8 -*-

def  get_other_direction(next_grid_direction):
    if  next_grid_direction in ['left','right']:
        return ['up','down']
    return ['left','right']

def manh(x1, y1, x2, y2):
    return abs(x1-x2) + abs(y1-y2)
def inner_grid_path_planner(x1, y1, x2, y2,v,allowed_directions):
    '''

    :param x1: 开始横坐标
    :param y1:
    :param x2: 结束横坐标
    :param y2:
    :return:
    '''
    current_distance = manh(x1, y1, x2, y2)
    dir2dxdy = {'left': (-1, 0), 'right': (1, 0), 'up': (0, -1), 'down': (0, 1)}
    result = []
    for direction in dir2dxdy:
        if direction in allowed_directions:
            dx,dy = dir2dxdy[direction]
            new_x = x1+ dx*v
            new_y = y1+ dy*v
            tmp_distance = manh(new_x, new_y, x2, y2)
            result.append((direction,tmp_distance))
    result = sorted(result,key=lambda x:x[1])
    # 如果新位置更近，return 这个方向
    if result[0][-1] < current_distance:
        return  result[0][0]
    return None    